package com.huluobo.hyj.hyjmodel.widet

import android.content.Context
import android.graphics.drawable.AnimationDrawable
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import com.huluobo.hyj.hyjmodel.R
import com.hyphenate.chat.EMMessage
import com.hyphenate.chat.EMTextMessageBody
import com.hyphenate.util.DateUtils
import kotlinx.android.synthetic.main.view_send_message_item.view.*
import java.util.*

/**
 *@author : USER
 *@date : 2020/9/18 14:05
 *@params :
 *@description:
 */
class SendMessageitemView(context:Context,attrs:AttributeSet? = null):
    RelativeLayout(context,attrs) {

    fun bindView(EmMeassage:EMMessage, showTimestamp: Boolean){
        updateMessage(EmMeassage)
        updateProgress(EmMeassage)
        updateTimestamp(EmMeassage, showTimestamp)
    }

    private fun updateTimestamp(emMessage: EMMessage, showTimestamp: Boolean) {
        if (showTimestamp) {
            timestamp.visibility = View.VISIBLE
            timestamp.text = DateUtils.getTimestampString(Date(emMessage.msgTime))
        } else timestamp.visibility = View.GONE

    }

    private fun updateProgress(emMessage: EMMessage) {
        emMessage.status().let {
            when (it) {
                EMMessage.Status.INPROGRESS -> {
                    sendMessageProgress.visibility = View.VISIBLE
                    sendMessageProgress.setImageResource(R.drawable.send_message_progress)
                    val animationDrawable = sendMessageProgress.drawable as AnimationDrawable
                    animationDrawable.start()
                }
                EMMessage.Status.SUCCESS -> {
                    sendMessageProgress.visibility = View.GONE
                }
                EMMessage.Status.FAIL -> {
                    sendMessageProgress.visibility = View.VISIBLE
                    sendMessageProgress.setImageResource(R.mipmap.msg_error)
                }
            }
        }
    }

    private fun updateMessage(emMeassage: EMMessage) {
        if(emMeassage.type == EMMessage.Type.TXT){
            sendMessage.text = (emMeassage.body as EMTextMessageBody).message
        }else{
            //其他消息类型 提示语  //其他消息类型写在这里else if
            sendMessage.text = context.getString(R.string.no_text_message)
        }
    }

    init {
        View.inflate(context, R.layout.view_send_message_item,this)
    }
}